package com.ali.ttt.netty.cluster.domain;

import com.ali.ttt.netty.common.domain.Client;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.CopyOnWriteArrayList;

@Data
@Slf4j
public class ClientList {

    private static ClientList singleInstance = new ClientList();

    private CopyOnWriteArrayList<Client> list = new CopyOnWriteArrayList<>();

    public static ClientList inst() {
        return singleInstance;
    }

    public void addServer(Client s) {
        list.add( s);
        log.info("服务端登录:host= " + s.getHost()
                + "prod=" + s.getProd()
                + "   共连接服务端: " + list.size());

    }

    public void removeServer(Client s) {
        for (Client server : list) {
            if(server.getHost().equals(s.getHost()) && server.getProd().equals(server.getProd())){
                list.remove(s);
                log.info("服务端断开:host= " + s.getHost()
                        + "prod=" + s.getProd()
                        + "   共连接服务端: " + list.size());
            }
        }
    }

    public boolean hasConnect(Client s) {
        for (Client server : list) {
            if(server.getHost().equals(s.getHost()) && server.getProd().equals(server.getProd())){
                return true;
            }
        }
        return false;
    }
}
